﻿using System;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using ES.Models;
using ePayment;

namespace ES.Controllers
{
    public class ProductController : BaseController
    {

        //
        // Ürün Listesini kategoriye göre getir.
        public ActionResult Index(string id, DefaultFilter filter)
        {
            ViewBag.Title = "Ürün Listesi";
            var list = Db.Products.AsQueryable();
            if (!string.IsNullOrEmpty(id))
            {
                list = list.Where(d => d.Category.SeoText.Equals(id));
                ViewBag.CategoryName = Db.Categories.Single(d => d.SeoText.Equals(id)).Text;
            }



            list = AddFilter(filter, list);

            return View(list.ToList());
        }




        public ActionResult Detail(string id)
        {
            var product = Db.Products.SingleOrDefault(d => d.Seo.Equals(id));
            if (product == null)
            {
                throw new Exception("ürün bulunamadı");
            }
            ViewBag.Title = product.Title;
            return View(product);
        }

        public ActionResult Sale(int id)
        {
            var product = Db.Products.SingleOrDefault(d => d.Id.Equals(id));
            if (product == null)
            {
                return RedirectToAction("Index");
            }
            return View(product);
        }
        [HttpPost]
        public ActionResult Sale(UserCardInfo cardInfo)
        {
            var product = Db.Products.SingleOrDefault(d => d.Id == cardInfo.Id);
            if (product == null)
            {
                return RedirectToAction("Index");
            }
            ViewBag.Result = "";
            var payment = new cc5payment
                              {
                                  host = "host",
                                  name = "",
                                  password = "",
                                  clientid = "",
                                  orderresult = 0,
                                  cardnumber = cardInfo.CardNumber,
                                  expmonth = cardInfo.ExpMonth.ToString(),
                                  expyear = cardInfo.ExpYear.ToString(),
                                  cv2 = cardInfo.SecurityNumber,
                                  currency = "949",
                                  chargetype = "Auth",
                                  ip = Request.ServerVariables["REMOTE ADDR"],
                                  subtotal = (((product.Price * product.Kdv)/100) + product.Price).ToString(),
                                  taksit = "1"
                              };

            string bankaSonuc = payment.processorder();
            string bankaHata = payment.errmsg;
            string bankaOid = payment.oid;
            string bankaAppr = payment.appr;
            string bankaProv = payment.code;

            if (bankaSonuc == "1")
            {
                if (bankaAppr == "Approved")
                {
                    ViewBag.Result = "Banka ödemeyi kabul etti.Ödeme işlemi başarı ile tamamlandı";
                }
                else if (bankaAppr == "Declined")
                {
                    ViewBag.Result = "Banka ödemeyi reddetti";
                }
                else
                {
                    ViewBag.Result = "Banka ile iletişim kurulamadı";
                }

            }

            var sale = new Sale
                           {
                               CargoPrice = 20,
                               CustomerId = CurrentCustomer.id,
                               ProductId = cardInfo.Id,
                               RecordDate = DateTime.Now,
                               SaleState = (short)SaleState.PendingCheck
                           };
            Db.Sales.InsertOnSubmit(sale);
// ReSharper disable AssignNullToNotNullAttribute
            Db.CustomerOfBaskets.DeleteOnSubmit(Db.CustomerOfBaskets.SingleOrDefault(d => d.CustomerId==CurrentCustomer.id && d.Id==product.Id));
// ReSharper restore AssignNullToNotNullAttribute
            Db.SubmitChanges();

  
            return RedirectToAction("Index");
        }


    }
}
